package wec_wallet.ebo.com.activity.register;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import org.json.JSONObject;

import bassproject.ebo.com.ebobass.activity.MyBaseActivity;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.NetState;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import bassproject.ebo.com.ebobass.utils.Lg;
import bassproject.ebo.com.ebobass.utils.StringUtil;
import butterknife.BindView;
import butterknife.ButterKnife;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.utils.Functions;
import wec_wallet.ebo.com.utils.PubRequest;
import wec_wallet.ebo.com.widget.MarqueeView;
import wec_wallet.ebo.com.widget.WaterImageView;

public class StartActivity extends MyBaseActivity {
    Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    leaveActivityToLogin();
                    break;
            }
        }
    };
    String ip, phone, code;
    Handler handlerLogin = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    PubRequest.login(StartActivity.this, ip, phone, code, new ListenerLogin(ip), false);
                    break;
            }
        }
    };

    //    String ip2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        DensityUtil.setHideVirtualKey(getWindow());
        setContentView(R.layout.activity_start);
        ButterKnife.bind(this);
        initActor();
        initViews();
    }

    //离开当前界面
    private void leaveActivityToLogin() {
        //未登录，进入登录页
        I_Share.setIsLogin(false);
        Intent intent = new Intent();
        intent.setClass(this, LoginActivity.class);
        startActivity(intent);
        finish();

    }

    @BindView(R.id.marquee_view)
    MarqueeView marqueeView1;
    @BindView(R.id.marquee_view2)
    MarqueeView marqueeView2;
    @BindView(R.id.marquee_view3)
    MarqueeView marqueeView3;
    @BindView(R.id.marquee_view4)
    MarqueeView marqueeView4;
    @BindView(R.id.iv_water01)
    WaterImageView iv_water01;
    @BindView(R.id.iv_water02)
    WaterImageView iv_water02;
    @BindView(R.id.iv_water03)
    WaterImageView iv_water03;
    private static final int speed_back = 23;
    private static final int speed_front = 35;

    private void initActor() {
        initMarqueeView(marqueeView1, R.drawable.wave01, speed_back, 0);
        initMarqueeView(marqueeView2, R.drawable.wave01, speed_back, -1);

        initMarqueeView(marqueeView3, R.drawable.wave02, speed_front, 0);
        initMarqueeView(marqueeView4, R.drawable.wave02, speed_front, -1);

        iv_water01.setVisibility(View.VISIBLE);
        iv_water02.setVisibility(View.VISIBLE);
        iv_water03.setVisibility(View.VISIBLE);
        iv_water01.startChangeAlpha(30);
        iv_water02.startChangeAlpha(60);
        iv_water03.startChangeAlpha(100);
    }

    private void initMarqueeView(MarqueeView mv, int drawable_id, int speed, int offset_x) {
        ImageView iv1 = new ImageView(this);
        iv1.setImageResource(drawable_id);
        mv.addViewInQueue(iv1);

        mv.setScrollSpeed(speed);
        mv.setScrollDirection(MarqueeView.RIGHT_TO_LEFT);
        mv.setViewMargin(0);
        mv.startScroll(offset_x == 0 ? 0 : mv.viewWidth + offset_x);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (iv_water01 != null)
            iv_water01.destroyDrawingCache();
        if (iv_water02 != null)
            iv_water02.destroyDrawingCache();
        if (iv_water03 != null)
            iv_water03.destroyDrawingCache();
    }

    @Override
    public void initViews() {
        if (I_Share.isLogin()) {
            //上次是已登录状态，调用登录接口
            phone = I_Share.readAccountUsername_Phone();
            code = I_Share.readAccountPasswordLogin();
            //账号是否为空
            if (phone == null || TextUtils.isEmpty(phone)) {
                IToast.show(getResources().getString(R.string.login_hint_input_username));
                return;
            }
            //登录密码是否为空
            if (code == null || TextUtils.isEmpty(code)) {
                IToast.show(getResources().getString(R.string.login_hint_input_password));
                return;
            }

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Lg.d("start check ip");
                    ip = NetState.getNetIp();
                    Lg.d("end check ip ip=" + ip);
                    if (StringUtil.isEmpty(ip)) {
                        leaveActivityToLogin();
                    } else {
                        handlerLogin.sendEmptyMessage(1);
                    }

                }
            }).start();

        } else {
            //未登录，进入登录页
            handler.sendEmptyMessageDelayed(1, 2000);
        }

    }

    @Override
    public void onClick(View v) {

    }


    //接口返回 start======================================================================================================================

    //登录
    class ListenerLogin implements IHttpPostListener {
        String ip;

        public ListenerLogin(String ip) {
            this.ip = ip;
        }

        @Override
        public void responseSuccess(String result) {
            if (null == result) {
                leaveActivityToLogin();
            }
            if (null != result) {
                try {
                    JSONObject resultObject = new JSONObject(result);
                    int error = resultObject.getInt("error");
                    String message = "";
                    if (resultObject.has("error_msg") && error != 0) {
                        message = resultObject.getString("error_msg");
//                        IToast.show(message);
                        I_Share.setIsLogin(false);
                        leaveActivityToLogin();
                    }
                    if (error == 0) {
                        //成功
//                        show_Toast("登录成功");
                        String user_id = resultObject.getString("id");
                        String mobile = resultObject.getString("mobile");
                        String nickname = resultObject.getString("nickname");
                        String avatar_path = resultObject.getString("avatar_path");//头像地址
                        String avatar = resultObject.getString("avatar");//头像文件夹
                        String token = resultObject.getString("remember_token");
                        String name = resultObject.getString("name");
                        int prefix = resultObject.getInt("prefix");
                        String is_remote = resultObject.getString("is_remote");
                        I_Share.saveAccount(mobile, null, null, token, user_id);
                        I_Share.setMobilePrefix(prefix);
                        I_Share.setNickname(nickname);
                        I_Share.setAvatar(avatar_path + avatar);
                        if (is_remote.equals("1")) {
                            I_Share.setIsLogin(false);
                            //ip异常
                            Intent intent = new Intent();
                            intent.setClass(StartActivity.this, ErrorLoginActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            I_Share.setIsLogin(true);
                            Functions.startMainActivity(StartActivity.this);
                        }


                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    Lg.d(getClass() + ",接口解析异常：\n" + e.toString());
                }
            }
        }

        @Override
        public void onTimeOut() {
            leaveActivityToLogin();
        }
    }


    //接口返回 end======================================================================================================================
}
